package com.guo.constellation;

import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentTransaction;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.widget.RadioGroup;

import com.google.gson.Gson;
import com.guo.constellation.bean.StarBean;
import com.guo.constellation.luckfrag.LuckFragment;
import com.guo.constellation.mefrag.MeFragment;
import com.guo.constellation.partnerfrag.PartnerFragment;
import com.guo.constellation.starfrag.StarFragment;
import com.guo.constellation.utils.AssetsUtils;

public class MainActivity extends AppCompatActivity implements RadioGroup.OnCheckedChangeListener {

    RadioGroup mainRg;
    //声明四个fragment
    Fragment starFrag, luckFrag, partnerFrag, meFrag;
    private FragmentManager manager;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        mainRg = findViewById(R.id.main_rg);
        mainRg.setOnCheckedChangeListener(this);

        //加载星座相关数据 /assets/xzcontent/xzcontent.json
        StarBean infoBean = loadData();
        Bundle bundle = new Bundle();
        bundle.putSerializable("info", infoBean);
        //通过activity想fragment里面传值是，Bean类里要实现serializable接口

        //创建碎片对象
        starFrag = new StarFragment();
        starFrag.setArguments(bundle);
        luckFrag = new LuckFragment();
        luckFrag.setArguments(bundle);
        partnerFrag = new PartnerFragment();
        partnerFrag.setArguments(bundle);
        meFrag = new MeFragment();
        meFrag.setArguments(bundle);
        //将四个碎片进行动态加载。这个加载有两个办法。（1）replace（2）add/hide/show

        addFragmentPage();
    }


    /*读取assets下的xzcontent.jason数据*/
    private StarBean loadData() {
        String json = AssetsUtils.getJsonFromAssets(this, "xzcontent/xzcontent.json");
        Gson gson = new Gson();
        StarBean infoBean = gson.fromJson(json, StarBean.class);
        AssetsUtils.saveBitmapFromAssets(this,infoBean);
        return infoBean;
    }


    /**
     * 将主页当中的碎片一起加入布局，有用的显示，无用的暂时隐藏
     */
    private void addFragmentPage() {
        //1.创建碎片管理者对象
        manager = getSupportFragmentManager();
        //2.创建碎片处理事务的对象
        FragmentTransaction transaction = manager.beginTransaction();
        //3.将四个碎片统一的添加到布局当中
        transaction.add(R.id.main_layout_center, starFrag);
        transaction.add(R.id.main_layout_center, luckFrag);
        transaction.add(R.id.main_layout_center, partnerFrag);
        transaction.add(R.id.main_layout_center, meFrag);
        //4.隐藏后面3个
        transaction.hide(partnerFrag);
        transaction.hide(luckFrag);
        transaction.hide(meFrag);
        ////5.提交碎片改变后的失误
        transaction.commit();
    }

    @Override
    public void onCheckedChanged(RadioGroup radioGroup, int i) {
        FragmentTransaction transaction = manager.beginTransaction();
        switch (i) {
            case R.id.main_rb_star:
                transaction.hide(partnerFrag);
                transaction.hide(luckFrag);
                transaction.hide(meFrag);
                transaction.show(starFrag);
                break;
            case R.id.main_rb_partner:
                transaction.hide(starFrag);
                transaction.hide(luckFrag);
                transaction.hide(meFrag);
                transaction.show(partnerFrag);
                break;
            case R.id.main_rb_luck:
                transaction.hide(partnerFrag);
                transaction.hide(starFrag);
                transaction.hide(meFrag);
                transaction.show(luckFrag);
                break;
            case R.id.main_rb_me:
                transaction.hide(partnerFrag);
                transaction.hide(luckFrag);
                transaction.hide(starFrag);
                transaction.show(meFrag);
                break;

            default:
                break;
        }
        transaction.commit();

    }
}
